import { Type } from "@angular/core";

export class PropertyItem {
    public itemType: Type<any>;
    public isValueRaw: boolean;
    public children: Map<string, PropertyItem>;

    constructor(private name: string, private val: any, private parent: PropertyItem) {

    }

    public resetChildren(): void {
        if (this.children) {
            this.children.clear();
        }
    }

    public setChild(childItem: PropertyItem | { propertyName: string, propertyValue: any, type?: Type<any> }) {
        if (!childItem) {
            return;
        }

        if (this.children == null) {
            this.children = new Map<string, PropertyItem>();
        }

        if (!(childItem instanceof PropertyItem)) {
            let item = new PropertyItem(childItem.propertyName, childItem.propertyValue, this);
            item.itemType = childItem.type;
            this.setChild(item);
            return;
        }

        if (this.children.has(childItem.name)) {
            this.children.delete(childItem.name);
        }

        this.children.set(childItem.name, childItem);
    }


    public getChildValue(childName: string): any {
        let child = this.getChild(childName);
        return child ? child.val : null;
    }

    public getChild(childName: string): PropertyItem {
        if (!this.children)
            return null;

        return this.children.get(childName);
    }
}
